/*
 * Copyright 2010 Christoph Widulle (christoph.widulle@googlemail.com)
 *
 * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the
 * License. You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS"
 * BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language
 * governing permissions and limitations under the License.
 */



package com.android1.amarena2d.nodes;

import com.android1.amarena2d.engine.AmarenaCamera;
import com.badlogic.gdx.math.Vector2;

public class BaseLeafEntity<T extends NodeEntity> extends BaseActionEntity implements LeafEntity<T> {

    protected int zOrder;
    protected T parent;

    @Override
    public void setParent(T parent) {
        this.parent = parent;
    }

    @Override
    public T getParent() {
        return parent;
    }

    @Override
    public int getZOrder() {
        return zOrder;
    }

    @Override
    public void setZOrder(int zOrder) {
        this.zOrder = zOrder;
    }

    @Override
    public float getAbsoluteX() {
        if (parent != null)
            return parent.getAbsoluteX() + getX();
        else
            return getX();
    }

    @Override
    public float getAbsoluteY() {
        if (parent != null)
            return parent.getAbsoluteY() + getY();
        else
            return getY();
    }

    @Override
    public void setAbsoluteY(float y) {
        if (parent != null) {
            super.setAbsoluteY(y - parent.getAbsoluteY());
        } else
            super.setAbsoluteY(y);
    }

    @Override
    public void setAbsoluteX(float x) {
        if (parent != null) {
            super.setAbsoluteY(x - parent.getAbsoluteX());
        } else
            super.setAbsoluteX(x);
    }

    @SuppressWarnings("unchecked")
    public void remove() {
        if (parent != null){
            parent.remove(this);
            parent = null;
        }
    }

    @Override
    public AmarenaCamera getCamera() {
        return parent.getCamera();
    }

    @Override
    public void translateToLocalCoords(Vector2 out) {
        if (parent != null) {
            parent.translateToLocalCoords(out);
        }
        if (transformDelegate == null || !transformDelegate.isTransform()) {
            out.x = out.x - getX();
            out.y = out.y - getY();
        } else {
            transformDelegate.transformCoords(out);
        }

    }
}
